package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.PersonalHomePageActivityContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseInfoTitleBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.MyCommonPublishCountBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserHomePageBean;
import com.kmproject.haifu.mvp.model.entity.beans.ViewStoreInfoBean;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsAttentionAdd;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsAttentionCancel;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/31/2019 10:34
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class PersonalHomePageActivityPresenter extends BasePresenter<PersonalHomePageActivityContract.Model, PersonalHomePageActivityContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public PersonalHomePageActivityPresenter(PersonalHomePageActivityContract.Model model, PersonalHomePageActivityContract.View rootView) {
        super(model, rootView);
    }

    public void getUserHomePageInfo(String userId) {
        mModel.getUserHomePageInfo(userId).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<UserHomePageBean>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<UserHomePageBean> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.showUserInfo(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                            mRootView.killMyself();
                        }
                    }
                });
    }

    public void myCommonPublishCount() {
        mModel.myCommonPublishCount("publish").compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<MyCommonPublishCountBean>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<MyCommonPublishCountBean> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.myCommonPublishCountSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                            mRootView.killMyself();
                        }
                    }
                });
    }

    public void viewStoreInfo(String storeId) {
        mModel.viewStoreInfo(storeId).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<ViewStoreInfoBean>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<ViewStoreInfoBean> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.viewStoreInfo(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                            mRootView.killMyself();
                        }
                    }
                });
    }

    public void follow(ReqParamsAttentionAdd reqParamsAttentionAdd) {
        mModel.follow(AppUtil.entityToMap(reqParamsAttentionAdd)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.followSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    public void unFollow(ReqParamsAttentionCancel reqParamsAttentionCancel) {
        mModel.unFollow(AppUtil.entityToMap(reqParamsAttentionCancel)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.unFollowSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 获取店铺选择类型
     */
    public void dictList4Category(String value) {
        mModel.dictList4Category(value, "").compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean)) {
                            mRootView.getCategorySuccess(listResultBean.getData());
                        } else {
                            mRootView.showMessage(listResultBean.getMessage());
                            mRootView.killMyself();
                        }
                    }
                });
    }

    public void getAreaList(String value) {
        mModel.getAreaList(Api.APP_DOMAIN_ZIDIAN + "/app/baseArea/getAreaTree", value, "").compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean)) {
                            mRootView.getAreaListSuccess(listResultBean.getData());
                        } else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }
                    }
                });
    }

    public void dictList4Screen(String value) {
        List<HouseInfoTitleBean> list = new ArrayList<>();
        HouseInfoTitleBean houseInfoTitleBean;
        for (int c = 0; c < 3; c++) {
            houseInfoTitleBean = new HouseInfoTitleBean("titleName" + c);
            HousePayWayBean bean2;
            HousePayWayBean bean3;
            List<HousePayWayBean> beanList2;
            List<HousePayWayBean> beanList3;
            beanList2 = new ArrayList<>();
            for (int j = 0; j < 15; j++) {
                bean2 = new HousePayWayBean();
                bean2.setName("name-" + c + "-" + j);
                beanList3 = new ArrayList<>();
                for (int k = 0; k < 10; k++) {
                    bean3 = new HousePayWayBean();
                    bean3.setName("name-" + c + "-" + j + "-" + k);
                    beanList3.add(bean3);
                }
                bean2.setChildren(beanList3);
                beanList2.add(bean2);
            }
            houseInfoTitleBean.setPayChileds(beanList2);

            HouseInfoTitleBean.HouseInfoConfiguration tHbean2;
            HouseInfoTitleBean.HouseInfoConfiguration tHbean3;
            List<HouseInfoTitleBean.HouseInfoConfiguration> tHbeanList2;
            List<HouseInfoTitleBean.HouseInfoConfiguration> tHbeanList3;
            tHbeanList2 = new ArrayList<>();
            for (int j = 0; j < 15; j++) {
                tHbean2 = new HouseInfoTitleBean.HouseInfoConfiguration();
                tHbean2.setName("name-" + c + "-" + j);
                tHbeanList3 = new ArrayList<>();
                for (int k = 0; k < 10; k++) {
                    tHbean3 = new HouseInfoTitleBean.HouseInfoConfiguration();
                    tHbean3.setName("name-" + c + "-" + j + "-" + k);
                    tHbeanList3.add(tHbean3);
                }
                tHbean2.setChildren(tHbeanList3);
                tHbeanList2.add(tHbean2);
            }
            houseInfoTitleBean.setChileds(tHbeanList2);
            list.add(houseInfoTitleBean);
        }

        mRootView.getScreenSuccess(list);


//        mModel.dictList4Screen(value, "").compose(RxUtils.applySchedulers(mRootView))
//                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HouseInfoTitleBean>>>(mErrorHandler) {
//                    @Override
//                    public void onNext(ResultBean<List<HouseInfoTitleBean>> listResultBean) {
//                        if (AppUtil.isSuccess(listResultBean)) {
//                            mRootView.getScreenSuccess(listResultBean.getData());
//                        } else {
//                            mRootView.showMessage(listResultBean.getMessage());
//                        }
//                    }
//                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
